package com.igeek.blog_parent_test.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.blog_parent_test.service.IUsersService;
import com.igeek.blog_parent_test.constant.MessageConstant;
import com.igeek.blog_parent_test.entity.Users;
import com.igeek.blog_parent_test.result.Result;
import com.igeek.blog_parent_test.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


//Swagger测试地址：http://localhost:8080/swagger-ui.html
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-04
 */
@RestController
@RequestMapping("admin/users")
@Api(tags = "用户接口管理")
public class UsersController {
@Autowired
    private IUsersService IUsersService;
@ApiOperation("分页条件查询用户")
    @GetMapping("findPage")
    public Result findPage(@ApiParam("分页参数") PageQueryVO vo){
    Page<Users>page=IUsersService.findPage(vo);
    if(page!=null){
        return new Result(true, MessageConstant.QUERY_USERS_SUCCESS,page);
    }
    return new Result(false,MessageConstant.QUERY_USERS_FAIL);
}
@ApiOperation("新增用户")
    @PostMapping("add")
    public Result add(@RequestBody Users users){
    boolean flag = IUsersService.save(users);
    if(flag) {
        return new Result(true, MessageConstant.ADD_USERS_SUCCESS);
    }
    return new Result(false,MessageConstant.ADD_USERS_FAIL);
}
@ApiOperation("通过ID查询用户信息")
    @GetMapping("{user_id}")
    public Result findOne(@PathVariable("user_id")Integer user_id){
    Users users = IUsersService.getById(user_id);
    if(ObjectUtils.isEmpty(users)) {
        return new Result(true, MessageConstant.QUERY_USERS_SUCCESS,users);
    }
    return new Result(true, MessageConstant.QUERY_USERS_FAIL);
}
    @ApiOperation("编辑用户")
    @PostMapping("update")
    public Result update(@RequestBody Users users){
        boolean flag = IUsersService.updateById(users);
        if(flag) {
            return new Result(true, MessageConstant.EDIT_USERS_SUCCESS);
        }
        return new Result(false,MessageConstant.EDIT_USERS_FAIL);
    }
    @ApiOperation("删除用户")
    @DeleteMapping("/{user_id}")
    public Result delete(@PathVariable("user_id")Integer user_id){
    boolean flag = IUsersService.del(user_id);
            if(flag){
                return new Result(true, MessageConstant.DELETE_USERS_SUCCESS);
            }
        return new Result(true, MessageConstant.DELETE_USERS_FAIL);
    }
}

